# /bin/bash
#
#

# shell full path
#
#
g_app_full_path=$(readlink -m $0);
g_app_root_dir=$(dirname $g_app_full_path);
g_virtual_terminal_socket_name="virtual.terminal.zbDiffShop.x"

function server_help()
{
    echo -e "description : restart a group of backend services process in the virtual terminal,Mainly including video monitoring\n";
    echo "developer   : ZLHRANHOU";
    echo "release     : 2023.10.25";
    echo "version     : 1.0.0.1";
    echo -e "\e[38;5:47musage\e[0m       : $g_app_full_path [help|stop|restart|status]";
}

#
#
#
function main()
{
    local v_cmd=$1
    
    #
    #
    case "$v_cmd" in
        ""|"--version"|"h"|"-h"|"--help"|"help")
             
            server_help;
            ;;
        "stop")
             
            server_stop;
            ;;
       "restart")
            server_stop; 
            server_restart;
            ;;
        "start_in_the_virtual_terminal")

            service_start;
            ;; 

        "status")

            server_status;
            ;; 
    esac;
}

function server_status()
{
    ps uax|grep -iE \("artisan command\:zbDiffShop"\)|grep -iv grep ;
}

function service_start()
{
    # virtual terminal log
    #
    #
    local v_virtual_terminal_log_file="$g_app_root_dir/virtual.terminal.log";
    # start php artisman
    #
    #
    echo "start php artisan ..." >> $v_virtual_terminal_log_file;
    $(local v_app_for_php_artisan_command_zb_log="$g_app_root_dir/php.artisan.zbDiffShop.log" ;cd $g_app_root_dir/../../; /www/server/php/80/bin/php artisan command:zbDiffShop 2>&1 1> $v_app_for_php_artisan_command_zb_log )
}

#
#
#
#
function server_restart()
{
    local v_virtual_terminal_pid=$( screen -ls|grep -i $g_virtual_terminal_socket_name|awk -F '.' '{print $1}'|sed 's/\s//g';);if [[ "" == "$v_virtual_terminal_pid" ]];then
        screen -dmS $g_virtual_terminal_socket_name;
    else
        :;
    fi;

    #
    #
    #
    local cmd="screen -S $g_virtual_terminal_socket_name -X screen sh $g_app_full_path start_in_the_virtual_terminal ;"; eval $cmd;
}



function server_stop()
{

    # kill php artisan
    #
    #
    local v_php_artisan_import_excel_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan command:zbDiffShop"|grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_zb_pids" ]];then :; else kill $v_php_artisan_zb_pids;fi;

    # kill screen session
    #
    #
    local v_virtual_terminal_pid=$( screen -ls|grep -i $g_virtual_terminal_socket_name|awk -F '.' '{print $1}'|sed 's/\s//g';);if [[ "" == "$v_virtual_terminal_pid" ]];then :; else kill $v_virtual_terminal_pid;fi;
}


#
# run
# 
main $@